package com.fanbai.score.common.base;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;


import com.fanbai.score.R;

import butterknife.ButterKnife;

/**
 * Created by Administrator on 2017/3/16.
 *
 * @author lizhipei
 */

public abstract class BaseFragment extends Fragment {

    protected BaseActivity ctx;
    protected BaseFragment frag;


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(getLayoutID(), container, false);
        //绑定fragment
        ButterKnife.bind(this, view);
        ctx = (BaseActivity) getActivity();
        frag = this;
        dealCommonPart(view);
        initView(view);
        initData();
        initListener();
        return view;
    }

    public RelativeLayout titleBar;
    TextView tvTitle, tvTitleLeft, tvTitleRight;
    /**
     * 处理公共逻辑
     */
    private void dealCommonPart(View view) {
        titleBar = (RelativeLayout)view.findViewById(R.id.titleBar);
        if(getTitleName() != null && !getTitleName().equals("")){
            tvTitle = (TextView) view.findViewById(R.id.titleName);
            if (tvTitle != null)
                tvTitle.setText(getTitleName());
        }
        ImageView tvTitleLeft = (ImageView) view.findViewById(R.id.titleLeft);
        if(tvTitleLeft != null){
            iniTitleLeft(tvTitleLeft);
        }
        TextView tvTitleRight = (TextView) view.findViewById(R.id.titleRight);
        if(tvTitleRight != null){
            iniTitleRight(tvTitleRight);
        }
    }

    public void setTvTitle(String title){
        if(tvTitle != null){
            tvTitle.setText(title);
        }
    }

    /**
     * 获取布局id
     *
     * @return 布局id
     */
    protected abstract int getLayoutID();

    /**
     * 获取标题显示框
     *
     * @return
     */
    public abstract String getTitleName();

    /**
     * 设置标题左侧文本
     *
     * @return
     */
    public abstract void iniTitleLeft(ImageView tv);

    /**
     * 设置标题右侧
     *
     * @return
     */
    public abstract void iniTitleRight(TextView tv);

    /**
     * 1/3初始化布局
     *
     * @param view 整个fragment控件的对象
     */
    protected abstract void initView(View view);

    /**
     * 2/3初始化数据
     */
    protected abstract void initData();

    /**
     * 3/3初始化监听
     */
    protected abstract void initListener();

}
